package com.eternal.org.controller;


import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.eternal.Exception.ResultEnum;
import com.eternal.Exception.ResultVo;
import com.eternal.operationLog.LogAnnotation;
import com.eternal.operationLog.LogMethodEnum;
import com.eternal.org.entity.OrganizeInfo;
import com.eternal.org.service.OrganizeInfoService;
import com.eternal.util.tree.entity.Tree;
import org.apache.poi.ss.formula.functions.T;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.List;
import java.util.NoSuchElementException;

/**
 * <p>
 * 基础组织机构表 前端控制器
 * </p>
 *
 * @author Eternal
 * @since 2018-10-28
 */
@RestController
@RequestMapping("/organizeInfo")
public class OrganizeInfoController {

    @Autowired
    private OrganizeInfoService organizeInfoService;

    @GetMapping("/queryOrganizeTree")
    @RequiresPermissions("organizeInfo_queryOrganizeTree")
    public ResultVo<List<Tree<OrganizeInfo>>> queryOrganizeTree() {
        List<Tree<OrganizeInfo>> organizeTree = organizeInfoService.getOrganizeTree();
        return new ResultVo<>(organizeTree);
    }

    @GetMapping("/queryOrganizeOrgIdTree")
    @RequiresPermissions("organizeInfo_queryOrganizeOrgIdTree")
    public ResultVo<List<Tree<OrganizeInfo>>> queryOrganizeOrgIdTree(String orgId) {
        List<Tree<OrganizeInfo>> organizeTree = organizeInfoService.getOrganizeTree(orgId);
        return new ResultVo<>(organizeTree);
    }

    @PostMapping("/addOrgnizeInfo")
    @RequiresPermissions("organizeInfo_addOrgnizeInfo")
    public ResultVo addOrgnizeInfo(OrganizeInfo organizeInfo) {
        System.out.println(organizeInfo.toString());
        boolean b = organizeInfoService.saveOrgnizeInfo(organizeInfo);
        return new ResultVo(b ? ResultEnum.SUCCESS : ResultEnum.ADD_ORGANIZE_ERROR);
    }

    /**
     * <p>  Excel导入组织机构 </p>
     *
     * @Description: TODO
     * @Param
     * @Return
     * @Author Eternal
     * @Date 2018/11/3 15:10
     */
    @PostMapping("/importExcelOrg")
    @RequiresPermissions("organizeInfo_importExcelOrg")
    public ResultVo importExcelOrg(MultipartFile file) {
        boolean excelOrg = organizeInfoService.importExcelOrg(file);
        return new ResultVo(excelOrg ? ResultEnum.SUCCESS : ResultEnum.IMPORT_ORGANIZE_ERROR);
    }

    @LogAnnotation(value = "组织下的人根据名称",method = LogMethodEnum.QUERY,apiName = "queryOrganizeTreeLikeName")
    @GetMapping("/queryOrganizeTreeLikeName")
    public ResultVo<List<Tree<OrganizeInfo>> > queryOrganizeTreeLikeName(String orgName) {
        List<Tree<OrganizeInfo>> organizeTreeLikeName = organizeInfoService.getOrganizeTreeLikeName(orgName);
        return new ResultVo<>(organizeTreeLikeName);
    }
}

